/**
 * Word文档导出工具
 */

// 简化版Word导出，不依赖外部库

/**
 * 导出为Word文档
 */
export async function exportToWord(htmlContent: string, filename: string = '文档'): Promise<void> {
  try {
    // 创建Word文档的HTML结构
    const wordHTML = createWordHTML(htmlContent);
    
    // 创建Blob对象
    const blob = new Blob([wordHTML], {
      type: 'application/msword;charset=utf-8'
    });
    
    // 创建下载链接
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = `${filename}.doc`;
    
    // 触发下载
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    // 清理URL对象
    URL.revokeObjectURL(url);
  } catch (error) {
    console.error('导出Word文档失败:', error);
    throw new Error('导出失败，请重试');
  }
}

/**
 * 创建Word格式的HTML
 */
function createWordHTML(content: string): string {
  return `
<html xmlns:o='urn:schemas-microsoft-com:office:office'
      xmlns:w='urn:schemas-microsoft-com:office:word'
      xmlns='http://www.w3.org/TR/REC-html40'>
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <!--[if gte mso 9]>
    <xml>
        <w:WordDocument>
            <w:View>Print</w:View>
            <w:Zoom>90</w:Zoom>
            <w:DoNotPromptForConvert/>
            <w:DoNotShowInsertionsAndDeletions/>
        </w:WordDocument>
    </xml>
    <![endif]-->
    <style>
        @page {
            margin: 2.54cm;
            mso-page-orientation: portrait;
        }
        
        body {
            font-family: '仿宋_GB2312', '仿宋', serif;
            font-size: 12pt;
            line-height: 1.5;
            margin: 0;
            padding: 0;
        }
        
        h1 {
            font-family: '黑体', 'SimHei', sans-serif;
            font-size: 16pt;
            font-weight: bold;
            text-align: left;
            margin-top: 12pt;
            margin-bottom: 12pt;
            line-height: 1.5;
        }
        
        h2 {
            font-family: '黑体', 'SimHei', sans-serif;
            font-size: 14pt;
            font-weight: bold;
            text-align: left;
            margin-top: 6pt;
            margin-bottom: 6pt;
            line-height: 1.5;
        }
        
        p {
            font-family: '仿宋_GB2312', '仿宋', serif;
            font-size: 12pt;
            text-align: justify;
            text-indent: 24pt;
            line-height: 1.5;
            margin-top: 0;
            margin-bottom: 6pt;
        }
        
        .document-title {
            font-family: '黑体', 'SimHei', sans-serif;
            font-size: 16pt;
            font-weight: bold;
            text-align: center;
            margin-top: 0;
            margin-bottom: 24pt;
            text-indent: 0;
        }
        
        ul, ol {
            margin-left: 24pt;
            margin-bottom: 6pt;
        }
        
        li {
            font-family: '仿宋_GB2312', '仿宋', serif;
            font-size: 12pt;
            line-height: 1.5;
            margin-bottom: 3pt;
        }
        
        table {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 12pt;
        }
        
        td, th {
            border: 1pt solid black;
            padding: 6pt;
            font-family: '仿宋_GB2312', '仿宋', serif;
            font-size: 12pt;
        }
        
        th {
            font-weight: bold;
            text-align: center;
        }
    </style>
</head>
<body>
    ${content}
</body>
</html>`;
}

/**
 * 使用docx库导出更高质量的Word文档（可选方案）
 */
export async function exportToDocx(content: string, title: string = '文档'): Promise<void> {
  // 这里可以集成docx库来生成更标准的Word文档
  // 由于需要额外依赖，暂时使用HTML转Word的方案
  return exportToWord(content, title);
}

/**
 * 检查浏览器支持
 */
export function checkBrowserSupport(): boolean {
  return typeof Blob !== 'undefined' && typeof URL !== 'undefined';
}

/**
 * 获取文件大小（字节）
 */
export function getDocumentSize(content: string): number {
  return new Blob([content]).size;
}
